package pt.nmfa.peste.session;

import pt.nmfa.peste.entity.*;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("channelsCategoryHome")
public class ChannelsCategoryHome extends EntityHome<ChannelsCategory> {

	@In(create = true)
	CategoryHome categoryHome;
	@In(create = true)
	ChannelsHome channelsHome;

	public void setChannelsCategoryId(ChannelsCategoryId id) {
		setId(id);
	}

	public ChannelsCategoryId getChannelsCategoryId() {
		return (ChannelsCategoryId) getId();
	}

	public ChannelsCategoryHome() {
		setChannelsCategoryId(new ChannelsCategoryId());
	}

	@Override
	public boolean isIdDefined() {
		if (getChannelsCategoryId().getId() == 0)
			return false;
		if (getChannelsCategoryId().getEpgpos() == null
				|| "".equals(getChannelsCategoryId().getEpgpos()))
			return false;
		return true;
	}

	@Override
	protected ChannelsCategory createInstance() {
		ChannelsCategory channelsCategory = new ChannelsCategory();
		channelsCategory.setId(new ChannelsCategoryId());
		return channelsCategory;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Category category = categoryHome.getDefinedInstance();
		if (category != null) {
			getInstance().setCategory(category);
		}
		Channels channels = channelsHome.getDefinedInstance();
		if (channels != null) {
			getInstance().setChannels(channels);
		}
	}

	public boolean isWired() {
		if (getInstance().getCategory() == null)
			return false;
		if (getInstance().getChannels() == null)
			return false;
		return true;
	}

	public ChannelsCategory getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
